<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id='app' v-cloak>
    <cpn @item-click="cpnClick"></cpn>
  </div>

  <template id="cpn">
    <div>
     <ul>
       <li v-for="item in categories">
         <button @click="getClass(item)">{{item.name}}</button>
       </li>
     </ul>
    </div>
  </template>
  <script src='../js/vue.js'></script>
  <script>
    // 子组件
    const cpn = {
      template: '#cpn',
      data() {
        return {
          categories: [{
            id: 1,
            name: '热门手机'
          }, {
            id: 2,
            name: '手机数码'
          }, {
            id: 3,
            name: '家用家电'
          }, {
            id: 4,
            name: '电脑办公'
          }, ]
        }
      },
      methods: {
        getClass(item) {
          // 发射事件：自定义事件
          this.$emit('item-click', item)
        }
      },
    }

    const app = new Vue({
      el: '#app',
      data: {
        message: 'hello Vue',
      },
      components: {
        cpn
      },
      methods: {
        cpnClick(item) {
          console.log('cpnClick', item);
        }
      }
    })
  </script>
  <style>
    [v-cloak] {
      display: none;
    }
  </style>
</body>

</html>